package com.sirap.fish.fetchers;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;

import com.google.common.collect.Lists;
import com.sirap.basic.component.Extractor;
import com.sirap.basic.component.Konstants;
import com.sirap.basic.json.Mist;
import com.sirap.basic.json.MistUtil;
import com.sirap.basic.tool.C;
import com.sirap.basic.tool.D;
import com.sirap.basic.util.Amaps;
import com.sirap.basic.util.DateUtil;
import com.sirap.basic.util.IOUtil;
import com.sirap.basic.util.MathUtil;
import com.sirap.basic.util.StrUtil;
import com.sirap.basic.util.XXXUtil;
import com.sirap.common.domain.Album;

public class SinaSportsImageFetchers {
	public static void main(String[] args) {
		Object va;
//		va = getAlbums(3);/
//		D.pl(countOfAlbum("https://www.fenun.cn/Item/Details/20912"));
//		va = getAlbumsByPage(1);
//		va = getAlbumsByPage(82);
		va = getAlbum("http://slide.news.sina.com.cn/z/slide_1_64237_313173.html#p=1");
		D.pjsp(va);
	}
	
	public static List<Map<String, Object>> getAlbums(int maxpage) {
		List<Map<String, Object>> allalbums = Lists.newArrayList();
		for(int page = 1; page <= maxpage; page++) {
//			allalbums.addAll(getAlbumsByPage(page));
//			break;
		}
		return allalbums;
	}
	
	public static Map<String, Object> getAlbumsByPage(int page) {
		Extractor<Map<String, Object>> frank = new Extractor<Map<String, Object>>() {
    		
			@Override
			public String getUrl() {
	        	String urlsports = "http://api.slide.news.sina.com.cn/interface/api_album_choice.php?ch=2&page=" + page;
				showFetching();
				return urlsports;
			}
//			@Override
			protected void fetchx() {
				String dir = "E:/KDB/tasks/1126_AlbumSina/sina.txt";
				source = IOUtil.readString(dir, Konstants.CODE_UTF8);
			}
			
			@Override
			protected void parse() {
				item = Amaps.newConcurrentHashMap();
				
				Mist top = MistUtil.ofJsonText(source);
				
				Object data = top.findBy("data");
				if(!List.class.isInstance(data)) {
					D.pl("Expect List here but: " + data);
					return;
				}

				String totalitems = top.findStringBy("total");
				int totalpage = MathUtil.totalPageOf(Integer.parseInt(totalitems), 16);
				item.put("totalpage", totalpage);

				XXXUtil.shouldBeTrue(data instanceof List);
				List items = (List)data;
				List<String> keys = StrUtil.split("name,url,img_url,img_198_132,createtime,img_count,short_intro");
				for(Object item : items) {
					Map<String, Object> row = Amaps.newLinkHashMap();
					Mist mist = MistUtil.ofMapOrList(item).asIs();
					for(String key : keys) {
						String temp = mist.findStringBy(key);
						temp = temp.replace("\\/", "/");
						String good = getPrettyText(temp);
						if(!temp.equals(good)) {
							D.pla(temp, good);
						}
						row.put(key, good);
					}
					mexItems.add(row);
				}
				
				item.put("photos", mexItems);
				C.listSome(mexItems, 3);
			}
		};
		
		return frank.process().getItem();
	}
	
	public static Album getAlbum(String albumurl) {
		Extractor<Album> frank = new Extractor<Album>() {
    		
			@Override
			public String getUrl() {
				useGBK().showFetching();
				return albumurl;
			}
//			@Override
			protected void fetchx() {
				String dir = "E:/KDB/tasks/1126_AlbumSina/harden.txt";
				source = IOUtil.readString(dir, Konstants.CODE_UTF8);
			}
			@Override
			protected void parse() {

				String regex1 = "\"createtime\":\"([\\d\\-\\s:]+)\"";
				String when = StrUtil.findFirstMatchedItem(regex1, source);
				
				Date whenDate = DateUtil.dateOf(when, DateUtil.DATE_TIME);
				
				String regex3 = "<meta itemprop=\"name\" content=\"([^\"]+)\"\\s+/>";
				String name = StrUtil.findFirstMatchedItem(regex3, source);
				
				String regex = "\"images\":(\\[.+?\\])\\s*,\\s*\"next_album\"";
				String images = StrUtil.findFirstMatchedItem(regex, source);
				
				Object data = MistUtil.ofJsonText(images).getCore();
				XXXUtil.shouldBeTrue(data instanceof List);
				List items = (List)data;
				List<String> keys = StrUtil.split("title,intro,image_url");
				
				List<Object> listObj = Lists.newArrayList();
				List<String> links = Lists.newArrayList();
				for(Object item : items) {
					Map<String, String> row = Amaps.newLinkHashMap();
					Mist mist = MistUtil.ofMapOrList(item);
					for(String key : keys) {
						String temp = mist.findStringBy(key);
						String good = temp.replace("\\/", "/");
						row.put(key, good);
					}
					listObj.add(row);
					links.add(row.get("image_url"));
				}
				
				item = Album.of(name, links).setTag("sina").setListObj(listObj).setUrl(albumurl).setWhen(when).setWhenDate(whenDate);
			}
		};
		
		return frank.process().getItem();
	}
	
	public static Integer countOfAlbum(String albumurl) {
		Extractor<Integer> frank = new Extractor<Integer>() {
    		
			@Override
			public String getUrl() {
				showFetching();
				return albumurl;
			}
			
			@Override
			protected void parse() {
				String regex = "data-large-src=\"(.+?)\"";
				Matcher ma = createMatcher(regex);
				item = 0;
				while(ma.find()) {
					item++;
				}
			}
		};
		
		return frank.process().getItem();
	}
}
